//
//  GGTSearchResultViewController.m
//  GGT
//
//  Created by Vision on 14-11-1.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTSearchResultViewController.h"
#import "DPRequest.h"
#import "GGTAppDelegate.h"
#import "GGTSearchTableViewCell.h"
#import "GGTFind_BusinessesModel.h"
#import "GGTBusinessViewController.h"
@interface GGTSearchResultViewController () <UITableViewDataSource,UITableViewDelegate, DPRequestDelegate,UIAlertViewDelegate>
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIButton *button3;

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UITableView *subTableView;
@property (nonatomic, retain) UITableView *tableView2;
@property (nonatomic, retain) UITableView *subTableView2;
@property (nonatomic, retain) UITableView *autoTableView;

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *subArr;
@property (nonatomic, retain) NSMutableArray *arr2;
@property (nonatomic, retain) NSMutableArray *subArr2;
@property (nonatomic, retain) NSMutableArray *autoArr;

@property (nonatomic, assign) BOOL tableViewState;
@property (nonatomic, assign) BOOL subTableViewState;
@property (nonatomic, assign) BOOL tableView2State;
@property (nonatomic, assign) BOOL subTableView2State;
@property (nonatomic, assign) BOOL autoTableViewState;

@property (nonatomic, assign) BOOL subArrState;
@property (nonatomic, assign) BOOL subArr2State;
@property (nonatomic, assign) BOOL autoArrState;




@property (nonatomic, assign) UIButton *cleanButton;
@property (nonatomic, assign) BOOL cleanButtonState;


@property (nonatomic, assign) int flag;
@property (nonatomic, retain) NSMutableDictionary *businessDetail;
@property (nonatomic, retain) NSMutableArray *businessArr;
@property (nonatomic, assign) NSInteger temp;//保存第一个tableView的indexPath.row

@property (nonatomic, retain) UITableView *searchDetailTableView;
@property (nonatomic, retain) GGTFind_BusinessesModel *searchDetail;
@property (nonatomic, retain) NSMutableDictionary *searchDIC;
@property (nonatomic, retain) NSDictionary *goodsDic;
@property (nonatomic, retain) NSDictionary *catDic;


@property (nonatomic, assign) BOOL switch1;
@property (nonatomic, assign) BOOL switch2;

@property (nonatomic, retain) NSString *categ;
@property (nonatomic, retain) NSString *region;
@property (nonatomic, assign) int autoNumber;
//空占位
@property (nonatomic, retain) UIImageView * tipImageView;
@property (nonatomic ,retain) UIAlertView *myTipAlertView;
@property (nonatomic ,assign) BOOL tipState;
@end


@implementation GGTSearchResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tableViewState = NO;
        self.subTableViewState = NO;
        self.tableViewState = NO;
        self.subTableView2State = NO;
        self.autoTableViewState = NO;
        self.cleanButtonState = NO;
        
        self.subArrState = NO;
        self.subArr2State = NO;
        self.autoArrState = NO;
        
        self.switch1 = YES;
        self.switch2 = NO;
        
        self.tipState = NO;
        
        self.subArr = [NSMutableArray array];//小城市
        self.arr = [NSMutableArray array];//大城市
        self.arr2 = [NSMutableArray array];//大分类
        self.subArr2 = [NSMutableArray array];//小分类
        self.autoArr = [NSMutableArray arrayWithObjects:@"默认", @"星级高优先", @"产品评价高优先", @"环境评价高优先", @"服务评价高优先", @"点评数量多优先", @"离传入经纬度坐标距离近优先", @"人均价格低优先", @"人均价格高优先", nil];//自动
        self.businessArr = [NSMutableArray array];//主界面
        
        self.flag = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *url = @"v1/metadata/get_regions_with_deals";
    NSString *params = [NSString stringWithFormat:@"city=%@",
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
    //self.view.backgroundColor = [UIColor yellowColor];
    // Do any additional setup after loading the view.
    [self filterButton];

    //团购tableView
    self.searchDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.searchDetailTableView.rowHeight = 120;
    self.searchDetailTableView.dataSource = self;
    self.searchDetailTableView.delegate = self;
    [self.view addSubview:self.searchDetailTableView];
    //[self.searchDetailTableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.arr.count;
    } else if (tableView == self.subTableView){
        return [[self.subArr objectAtIndex:self.temp] count];
    } else if (tableView == self.tableView2) {
        return [self.arr2 count];
    
    } else if (tableView == self.subTableView2) {
            return [[self.subArr2 objectAtIndex:self.temp]count];
    } else if (tableView == self.autoTableView) {
            return [self.autoArr count];
    } else {
        
        return [self.businessArr count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
            cell.textLabel.text = self.arr[indexPath.row];
            cell.contentView.backgroundColor = [UIColor lightGrayColor];
        return cell;
    } else if (tableView == self.subTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subTableView"];
        NSMutableArray *arr = [self.subArr objectAtIndex:self.temp];
        
        
        cell.textLabel.text = [arr objectAtIndex:indexPath.row] ;
        return cell;
    } else  if(tableView == self.tableView2)  {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView2"];
        cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
        return cell;
    
    } else if (tableView == self.subTableView2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subTableView2"];
        cell.textLabel.text = [[self.subArr2 objectAtIndex:self.temp] objectAtIndex:indexPath.row];
        
        return cell;
    } else if (tableView == self.autoTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"autoTableView"];
        cell.textLabel.text = [self.autoArr objectAtIndex:indexPath.row];
        return cell;
    } else {
        GGTSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"teamBuy"];
        if (cell == nil) {
            cell = [[[GGTSearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"teamBuy"]autorelease];
        }
        if (self.subArrState == NO && self.subArr2State == NO && self.autoArrState == NO) {
           
                GGTSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"11"];
                if (cell == nil) {
                    cell = [[[GGTSearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"11"]autorelease];
                }
            GGTFind_BusinessesModel *findBusiness = [self.businessArr objectAtIndex:indexPath.row];
            cell.findModel = findBusiness;
            
            return cell;
        } else if (self.autoArrState == NO) {
            GGTFind_BusinessesModel *findBusiness = [self.businessArr objectAtIndex:indexPath.row];
            cell.findModel = findBusiness;
            return cell;
        } else if (self.autoArrState == YES){
            GGTFind_BusinessesModel *findBusiness = [self.businessArr objectAtIndex:indexPath.row];
            cell.findModel = findBusiness;
            return cell;
        } else {
            return  cell;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.tableView) {
        [self.subTableView removeFromSuperview];
        self.temp = indexPath.row ;
        self.subTableView = [[UITableView alloc]initWithFrame:CGRectMake(160, 65, 160, 290) style:UITableViewStylePlain];
        self.subTableView.delegate = self;
        self.subTableView.dataSource = self;
        [self.view addSubview:self.subTableView];
        [self.subTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subTableView"];
       
        self.subTableViewState = YES;

    } else if (tableView == self.subTableView) {
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"region" object:[[self.subArr objectAtIndex:self.temp] objectAtIndex:indexPath.row] userInfo:nil];
        self.subArrState = YES;
 [self cleanView];
    } else if (tableView == self.tableView2){
    
        [self.subTableView2 removeFromSuperview];
        self.temp = indexPath.row ;
        self.subTableView2 = [[UITableView alloc]initWithFrame:CGRectMake(160, 65, 160, 290) style:UITableViewStylePlain];
        self.subTableView2.delegate = self;
        self.subTableView2.dataSource = self;
        [self.view addSubview:self.subTableView2];
        [self.subTableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subTableView2"];
        self.subTableView2State = YES;
    } else if (tableView == self.subTableView2) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"categ" object:[[self.subArr2 objectAtIndex:self.temp] objectAtIndex:indexPath.row] userInfo:nil];
        self.subArr2State = YES;
    [self cleanView];
    } else if (tableView == self.autoTableView) {
        NSString *str = [NSString stringWithFormat:@"%d", indexPath.row + 1];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"auto" object:str userInfo:nil];
        self.autoArrState = YES;
    [self cleanView];
    
    } else {
        GGTBusinessViewController *businessVC = [[GGTBusinessViewController alloc]init];
        GGTFind_BusinessesModel *dic = [self.businessArr objectAtIndex:indexPath.row];
        businessVC.businessDetail = dic.businessId;
        [self.navigationController pushViewController:businessVC animated:YES];
    }
}
- (void)filterButton
{
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(0, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button1.layer.cornerRadius = 10;
    self.button1.layer.borderWidth = 0.5;
    [self.button1 setTitle:@"全部商区" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button1];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(self.view.frame.size.width / 3 + 1, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button2.layer.cornerRadius = 10;
    self.button2.layer.borderWidth = 0.5;
    [self.button2 setTitle:@"全部频道" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button2];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(self.view.frame.size.width / 3 * 2 + 2, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button3.layer.cornerRadius = 10;
    self.button3.layer.borderWidth = 0.5;
    [self.button3 setTitle:@"智能排序" forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button3];
    
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error
{
    //self.resultTextView.contentOffset = CGPointZero;
    NSLog(@"注意参数或地址填写错误:%@",[error description]);
    
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{
    
    //[self.tipImageView removeFromSuperview];
//解析按钮数据
    if (self.switch1 == YES && self.switch2 == NO) {
        //self.resultTextView.contentOffset = CGPointZero;
        //地域
        if (self.flag % 3 == 0) {
            NSMutableArray *city = [result objectForKey:@"cities"];
            for (NSDictionary *dic in city) {
                NSMutableArray *districts = [dic objectForKey:@"districts"];
                for (NSDictionary *districtsDic in districts) {
                    NSString *districts = [districtsDic objectForKey:@"district_name"];
                    NSMutableArray *neighborhoods = [districtsDic objectForKey:@"neighborhoods"];
                    
                    [self.subArr addObject:neighborhoods];
                    [self.arr addObject:districts];
                }
            }
            
            NSString *url = @"v1/business/find_businesses";
            NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@",self.keyword,
                                [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
            [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        } else if (self.flag % 3 == 1) {
            //未点击任何按钮前的界面
            self.businessDetail = result;
            if (result == nil) {
                NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
                [center postNotificationName:@"no" object:nil userInfo:nil];
            }
            NSMutableArray *array = [self.businessDetail objectForKey:@"businesses"];
            for (NSDictionary *dic in array) {
                GGTFind_BusinessesModel *business = [[GGTFind_BusinessesModel alloc]initWithDictionary:dic];
                [self.businessArr addObject:business];
            }
            if (self.businessArr.count == 0) {
                [self hereIsATip];
            }
            NSString *url = @"v1/metadata/get_categories_with_deals";
            NSString *params = nil;
            [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        } else if (self.flag % 3 == 2) {
            NSMutableArray *categoriesArr = [result objectForKey:@"categories"];
            for (NSDictionary *categoryDic in categoriesArr) {
                NSString *category_name = [categoryDic objectForKey:@"category_name"];
                NSMutableArray *subcategories = [categoryDic objectForKey:@"subcategories"];
                [self.subArr2 addObject:subcategories];
                [self.arr2 addObject:category_name];
            }
            self.switch1 = NO;
            self.switch2 = YES;
        }
        NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
        [notiCenter addObserver:self selector:@selector(receiveNotfication:) name:@"region" object:nil];
        NSNotificationCenter *notiCenter1 = [NSNotificationCenter defaultCenter];
        [notiCenter1 addObserver:self selector:@selector(receiveNot:) name:@"categ" object:nil];
        NSNotificationCenter *notiCenter2 = [NSNotificationCenter defaultCenter];
        [notiCenter2 addObserver:self selector:@selector(receive:) name:@"auto" object:nil];
        [self.searchDetailTableView reloadData];
        [self.tableView reloadData];
        [self.tableView2 reloadData];
//解析点击按钮后的数据
    } else if (self.switch1 == NO && self.switch2 == YES) {
            NSMutableArray *busindss = [result objectForKey:@"businesses"];
            [self.businessArr removeAllObjects];
            for (NSDictionary *dic in busindss) {
                GGTFind_BusinessesModel *businessModel = [[GGTFind_BusinessesModel alloc]initWithDictionary:dic];
                [self.businessArr addObject:businessModel];
            }
            if (self.businessArr.count == 0) {
                [self hereIsATip];
            }
        [self.searchDetailTableView reloadData];
        [self.tableView reloadData];
        [self.tableView2 reloadData];
    }
     self.flag++;
}

- (void)hereIsATip
{
    
//    
//     self.tipImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height)];
//    self.tipImageView.image = [UIImage imageNamed:@"12.jpg"];
//    [self.view addSubview:self.tipImageView];
//    [self.tipImageView release];
    if (self.tipState == NO) {
        
        self.myTipAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前筛选条件下暂无商家" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        self.myTipAlertView.delegate = self;
        [self.myTipAlertView show];
        self.tipState = YES;
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.tipState = NO;
}
//点击按钮后开始解析
- (void)receiveNotfication:(NSNotification *)noti
{
    self.region = noti.object;
    NSString *url1 = @"v1/business/find_businesses";
    if (self.subArr2State == YES) {
        NSString *params1 = [NSString stringWithFormat:@"category=%@&city=%@&region=%@",self.categ,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.subArr2State == NO) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&region=%@",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.autoArrState == YES) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&region=%@&sort=%d",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object, self.autoNumber];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.autoArrState == NO) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&region=%@&sort=1",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.autoArrState == YES && self.subArr2State == YES) {
        NSString *params1 = [NSString stringWithFormat:@"category=%@&city=%@&region=%@&sort=%d",self.categ,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object, self.autoNumber];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    }
   
    
    
}
- (void)receiveNot:(NSNotification *)noti
{
    self.categ = noti.object;
    NSString *url1 = @"v1/business/find_businesses";
    if(self.subArrState == NO) {
    NSString *params1 = [NSString stringWithFormat:@"city=%@&category=%@",
                         [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"], noti.object];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.subArrState == YES) {
        NSString *params1 = [NSString stringWithFormat:@"city=%@&region=%@&category=%@",
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],self.region, noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.autoArrState == YES) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&region=%@&sort=%d",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object, self.autoNumber];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.autoArrState == NO) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&region=%@&sort=1",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    }

}
- (void)receive:(NSNotification *)noti
{
    self.autoNumber = [noti.object intValue];
    NSString *url1 = @"v1/business/find_businesses";
    if (self.subArr2State == YES) {
        NSString *params1 = [NSString stringWithFormat:@"category=%@&city=%@&sort=%d",self.categ,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"], self.autoNumber];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.subArr2State == NO) {
        NSString *params1 = [NSString stringWithFormat:@"keyword=%@&city=%@&sort=%d",self.keyword,
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"], self.autoNumber];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } if(self.subArrState == NO) {
        NSString *params1 = [NSString stringWithFormat:@"city=%@&category=%@",
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"], noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    } else if (self.subArrState == YES) {
        NSString *params1 = [NSString stringWithFormat:@"city=%@&region=%@&category=%@",
                             [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"],self.region, noti.object];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url1 paramsString:params1 delegate:self];
    }
    
}
- (void)cleanView
{
    if (self.tableViewState == YES) {
        [self.tableView removeFromSuperview];
        self.tableViewState = NO;
    }
    if (self.subTableViewState == YES) {
        
        [self.subTableView removeFromSuperview];
        self.subTableViewState = NO;
    }
    if (self.tableView2State == YES) {
        [self.tableView2 removeFromSuperview];
        self.tableView2State = NO;
    }
    if (self.subTableView2State == YES) {
        [self.subTableView2 removeFromSuperview];
        self.tableView2State = NO;
    }
    if (self.autoTableViewState == YES) {
        [self.autoTableView removeFromSuperview];
        self.autoTableViewState = NO;
    }
    if (self.cleanButtonState == YES) {
        
        [self.cleanButton removeFromSuperview];
        self.cleanButtonState = NO;
    }
}
- (void)creatCleanButton
{
    self.cleanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cleanButton.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
    self.cleanButton.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.5];
    [self.cleanButton addTarget:self action:@selector(cleanButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cleanButton];
    self.cleanButtonState = YES;

}


- (void)cleanButtonClicked:(UIButton *)button
{
    [self cleanView];
}

- (void)button1Clicked:(UIButton *)button
{
    
    if (self.tableViewState == NO) {
        [self creatCleanButton];
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 160, 290) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource =self;
        self.tableView.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.tableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.tableView];
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"tableView"]];
        [UIView animateWithDuration:.1 animations:^{
            self.tableView.frame = CGRectMake(0, 65, 160, 290);
        }];
        
        
        //[self.businessArr removeAllObjects];
        self.tableViewState = YES;
    } else {
        [self cleanView];
    }
}
- (void)button2Clicked:(UIButton *)button
{
    
    if (self.tableView2State == NO) {
        [self creatCleanButton];
        self.tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 160, 290) style:UITableViewStylePlain];
        self.tableView2.delegate = self;
        self.tableView2.dataSource =self;
        self.tableView2.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.tableView2.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.tableView2];
        [self.tableView2 registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"tableView2"]];
        [UIView animateWithDuration:.1 animations:^{
            self.tableView2.frame = CGRectMake(0, 65, 160, 290);
        }];

        
        
        
        self.tableView2State = YES;
    } else {
        [self cleanView];
    }
}
- (void)button3Clicked:(UIButton *)button
{
    
    if (self.autoTableViewState == NO) {
        [self creatCleanButton];
        self.autoTableView = [[UITableView alloc]initWithFrame:CGRectMake(-160, 54, 280, 290) style:UITableViewStylePlain];
        self.autoTableView.delegate = self;
        self.autoTableView.dataSource =self;
        self.autoTableView.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:0.1];
        self.autoTableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.autoTableView];
        [self.autoTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"autoTableView"]];
        [UIView animateWithDuration:.1 animations:^{
            self.autoTableView.frame = CGRectMake(0, 65, 280, 290);
        }];
        self.autoTableViewState = YES;
    } else {
        [self cleanView];
    }
}
- (void)dealloc
{
    [_tableView release];
    [_subTableView release];
    [_arr release];
    [_subArr release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
